<?php
/**
 *产品销售数量平均值计算公式模型,现只用备货规则中
 *@author shipanpan
 */
class Cron_Model_Backup_SalesMeanStrategy extends Model_Base {
	private $_productSalesModel;
	private $_useCalculateStrategyIndex;

	public function __construct($calculateStrategyIndex) {
		$this->_useCalculateStrategyIndex = $calculateStrategyIndex;
		$this->_productSalesModel = new Application_Model_ProductSales();
	}

	/**
	 * 获取产品销售数量平均值
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]                [description]
	 */
	public function getMean($goodsSn, $warehouseCode) {
		$methodName = "_strategy" . $this->_useCalculateStrategyIndex;
		return $this->$methodName($goodsSn, $warehouseCode);
	}

	/**
	 * 30天,15天,7天的平均销量分别为a,b,c,当c最大时,平均销量为c,当c非最大时,平均销量为0.4c+0.6b[销量指备货销量]
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]          [description]
	 */
	private function _strategy1($goodsSn, $warehouseCode) {
		$productSalesInfo=$this->_productSalesModel->getOne($goodsSn, $warehouseCode);
		$thirtyDaySalesQuantity = $productSalesInfo['sale_30']??0;
		$thirtyDaySalesQuantityMean = $thirtyDaySalesQuantity / 30;

		$fifteenDaySalesQuantity = $productSalesInfo['sale_15']??0;;
		$fifteenDaySalesQuantityMean = $fifteenDaySalesQuantity / 15;

		$sevenDaySalesQuantity = $productSalesInfo['sale_7']??0;;
		$sevenDaySalesQuantityMean = $sevenDaySalesQuantity / 7;

		if (($sevenDaySalesQuantityMean > $thirtyDaySalesQuantityMean) && ($sevenDaySalesQuantityMean > $fifteenDaySalesQuantityMean)) {
			return $sevenDaySalesQuantityMean;
		}

		return 0.4 * $sevenDaySalesQuantityMean + 0.6 * $fifteenDaySalesQuantityMean;
	}

	/**
	 * 30天平均销量,当产品上架时间小于30天时,取上架时间段内的平均销量[销量指备货销量]
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]          [description]
	 */
	private function _strategy2($goodsSn, $warehouseCode) {
		$thirtyDaySalesQuantity = $this->_productSalesModel->goodsBackupSalesQuantity($goodsSn, $warehouseCode, Application_Model_ProductSales::THIRTY_DAY);
		if ($thirtyDaySalesQuantity === false) {
			return $this->_error($this->_productSalesModel->getErrorMsg());
		}
		return $thirtyDaySalesQuantity / 30;
	}

	/**
	 * 前15天销量*2+前7天销量*4.3)/60[销量指备货销量]
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]          [description]
	 */
	private function _strategy3($goodsSn, $warehouseCode) {
		$productSalesInfo=$this->_productSalesModel->getOne($goodsSn, $warehouseCode);
		$fifteenDaySalesQuantity = $productSalesInfo['sale_15']??0;
		$sevenDaySalesQuantity = $productSalesInfo['sale_7']??0;
		return ($fifteenDaySalesQuantity * 2 + $sevenDaySalesQuantity * 4.3) / 60;
	}

	/**
	 * 平均销量=前一周日均销量【销量指备货销量】
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]          [description]
	 */
	private function _strategy4($goodsSn, $warehouseCode) {
		$sevenDayStockSalesQuantity = $this->_productSalesModel->goodsBackupSalesQuantity($goodsSn, $warehouseCode, Application_Model_ProductSales::SEVEN_DAY);
		if ($sevenDayStockSalesQuantity === false) {
			return $this->_error($this->_productSalesModel->getErrorMsg());
		}
		return $sevenDayStockSalesQuantity / 7;
	}

	/**
	 * 平均销量=(前第一周销量*0.4+前第二周销量*0.6)/7)[销量指备货销量]
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 仓库代码
	 * @return [type]          [description]
	 */
	private function _strategy5($goodsSn, $warehouseCode) {
		$productSalesInfo=$this->_productSalesModel->getOne($goodsSn, $warehouseCode);
		$theSecondWeekSalesQuantity = $productSalesInfo['sale_second_week']??0;

		$sevenDaySalesQuantity = $productSalesInfo['sale_7']??0;;
	
		return ($sevenDaySalesQuantity * 0.4 + $theSecondWeekSalesQuantity * 0.6) / 7;
	}

	/**
	 *  平均销量=前一周日均销量[销量指真实销量]
	 * @param  [type] $goodsSn [description]
	 * @return [type]          [description]
	 */
	private function _strategy6($goodsSn, $warehouseCode) {
		$sevenDaySalesQuantity = $this->_productSalesModel->goodsRealSalesQuantity($goodsSn, $warehouseCode, Application_Model_ProductSales::SEVEN_DAY);
		return $sevenDaySalesQuantity / 7;
	}

	/**
	 * 获取公式描述
	 * @param  [type] $strategyIndex 公式编号
	 * @return string
	 */
	public static function description(int $strategyIndex) {
		$descriptionArr = [
			1 => '公式1(30天,15天,7天的平均销量分别为a,b,c,当c最大时,平均销量为c,当c非最大时,平均销量为0.4c+0.6b)',
			'公式2(30天平均销量,当产品上架时间小于30天时,取上架时间段内的平均销量)',
			'公式3((前15天销量*2+前7天销量*4.3)/60)',
			'公式4(平均销量=前一周日均销量)【备货销量】',
			'公式5(平均销量=(前第一周销量*0.4+前第二周销量*0.6)/7)',
			'公式6 (平均销量=前一周日均销量)【真实销量】',
			'公式7(平均销量=前3天的日均销量*(昨日销量/前日销量))【真实销量】',
		];

		return $descriptionArr[$strategyIndex] ?? "";
	}
}